/**
 * @param {number[]} A
 * @return {number}
 */
const longestMountain = function (A) {
  const N = A.length
  let ans = 0,
    base = 0
  while (base < N) {
    let end = base
    // if base is a left-boundary
    if (end + 1 < N && A[end] < A[end + 1]) {
      // set end to the peak of this potential mountain
      while (end + 1 < N && A[end] < A[end + 1]) end++

      // if end is really a peak..
      if (end + 1 < N && A[end] > A[end + 1]) {
        // set end to the right-boundary of mountain
        while (end + 1 < N && A[end] > A[end + 1]) end++
        // record candidate answer
        ans = Math.max(ans, end - base + 1)
      }
    }

    base = Math.max(end, base + 1)
  }

  return ans
}
